package com.yogee.topseller.activity.login.view.impl;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.model.bean.SMSbean;
import com.yogee.topseller.activity.login.presenter.RegisterPresenter;
import com.yogee.topseller.activity.login.view.IRegisterView;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends HttpActivity implements IRegisterView {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.cet_phone)
    ClearEditText cetPhone;
    @BindView(R.id.cet_check_code)
    ClearEditText cetCheckCode;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.cet_pwd)
    ClearEditText cetPwd;
    @BindView(R.id.cet_steward_code)
    ClearEditText cetStewardCode;
    @BindView(R.id.confirm)
    TextView confirm;

    private RegisterPresenter registerPresenter;

    private int mRadomNumber = 0;

    private CountDownTimer timer;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        registerPresenter = new RegisterPresenter(this);
        mRadomNumber = AppUtil.getRandomNumber(6);
    }

    public Boolean validate() {
        if (TextUtils.isEmpty(cetPhone.getText())) {
            ToastUtils.showToast(this, "请输入手机号");
            return false;
        } else if (!AppUtil.validatePhone(cetPhone.getText().toString())) {
            ToastUtils.showToast(this, "请输入正确的手机号");
            return false;
        } else if (TextUtils.isEmpty(cetCheckCode.getText())) {
            ToastUtils.showToast(this, "请获取验证码");
            return false;
        } else if (!cetCheckCode.getText().toString().equals(String.valueOf(mRadomNumber))) {
            ToastUtils.showToast(this, "验证码输入错误");
            return false;
        } else if (TextUtils.isEmpty(cetPwd.getText())) {
            ToastUtils.showToast(this, "请输密码");
            return false;
        } else if (cetPwd.getText().toString().length() < 4 || cetPwd.getText().toString().length() > 20) {
            ToastUtils.showToast(this, "请填写4-20位密码");
            return false;
        }
        return true;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void newUserRegistration() {

    }

    @Override
    public void registerSuccess(String userId,String falgs,String day) {
        ToastUtils.showToast(this, "注册成功");
        Intent intent = new Intent(RegisterActivity.this, RegisterLabelActivity.class);
        intent.putExtra("userId", userId);
        startActivity(intent);
        this.finish();
    }

    @Override
    public void checkCode(SMSbean smSbean) {
        if (Integer.parseInt(smSbean.getError_code()) == 0) {
            countDownTimer();
            ToastUtils.showToast(this, "验证码已经发送");
        } else {
            getCode.setClickable(true);
            ToastUtils.showToast(this, smSbean.getError_code() + ":" + smSbean.getReason());
        }
    }

    @Override
    public void returnCheck() {
        registerPresenter.newUserRegistration(cetPhone.getText().toString()
                , cetPwd.getText().toString()
                , cetStewardCode.getText().toString());
    }

    @OnClick({R.id.back, R.id.get_code, R.id.confirm, R.id.agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                if (AppUtil.isFastDoubleClick(500)) {
                    return;
                }
                onBackPressed();
                break;
            case R.id.get_code:
                if (AppUtil.isFastDoubleClick(500)) {
                    return;
                }
                if ("重新发送".equals(getCode.getText().toString())) {
                    mRadomNumber = AppUtil.getRandomNumber(6);
                }
                if (TextUtils.isEmpty(cetPhone.getText())) {
                    ToastUtils.showToast(this, "请输入手机号");
                    return;
                } else if (!AppUtil.validatePhone(cetPhone.getText().toString())) {
                    ToastUtils.showToast(this, "请输入正确的手机号");
                    return;
                } else {
                    getCode.setTextColor(getResources().getColor(R.color.gray));
                    getCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.corners5_stroke1gray));
                    getCode.setClickable(false);
                    registerPresenter.sendSMS(RegisterActivity.this, cetPhone.getText().toString(), mRadomNumber);
                }
                break;
            case R.id.confirm:
                if (AppUtil.isFastDoubleClick(500)) {
                    return;
                }
                if (validate()) {
                    registerPresenter.newUserRegistration(cetPhone.getText().toString()
                            , cetPwd.getText().toString()
                            , cetStewardCode.getText().toString());
                }
                break;
            case R.id.agreement:
                if (AppUtil.isFastDoubleClick(1000)) {
                    return;
                }
                startActivity(new Intent(RegisterActivity.this, TermOfService.class));
                break;
        }
    }


    public void countDownTimer() {

        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                getCode.setText("重新发送" + "(" + millisUntilFinished / 1000 + "s)");
                getCode.setTextColor(getResources().getColor(R.color.gray));
                getCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.corners5_stroke1gray));
                getCode.setClickable(false);
            }

            @Override
            public void onFinish() {
                getCode.setText("重新发送");
                getCode.setTextColor(getResources().getColor(R.color.theme_color));
                getCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.corners5_stroke1_theme));
                getCode.setClickable(true);
            }
        };
        timer.start();
    }
}
